import axios from 'axios'
import { Message } from 'element-ui'
import { getContext } from '@/main'
import { getLanguage, getToken, setIsDemoAccount, setToken } from '@/util/storage'

const noExistWhiteRoute = [
  window.VUE_APP_BASE_API + '/weather/get'
]

// create an axios instance
const service = axios.create({
  baseURL: window.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 30 * 1000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    config.headers['access-token'] = getToken()
    config.headers['language'] = getLanguage()

    if (config.method === 'post') {
      if (!config.data) {
        config.data = {}
      }
    }
    return config
  },
  error => {
    // do something with request error
    console.error(error) // for debug
    if (error.message.includes('Network Error')) {
      Message({
        message: '网络出错',
        type: 'error',
        showClose: true,
        duration: 5 * 1000
      })
    }
    return Promise.reject(error)
  }
)

// vo interceptor
service.interceptors.response.use(
  response => {
    const that = getContext()
    const res = response.data
    if (res.code !== '200') {
      if (!that) {
        Message({
          message: '请求失败，请刷新重试',
          type: 'error',
          showClose: true,
          duration: 5 * 1000
        })
        return res
      } else if (res.code === '201') {
        if (noExistWhiteRoute.indexOf(response.config.url) !== -1) {
          Message({
            message: res.msg,
            type: 'error',
            showClose: true,
            duration: 5 * 1000
          })
        }
      } else if (res.code === '401') {
        Message({
          message: that.$t('logout'),
          type: 'error',
          showClose: true,
          duration: 5 * 1000
        })
        setToken(null)
        setIsDemoAccount(null)
        // console.log(document.location.protocol + '//' + getHostName() + ':' + window.location.port + '/#/login')
        window.location.reload()
      } else {
        Message({
          message: res.msg,
          type: 'error',
          showClose: true,
          duration: 5 * 1000
        })
      }
    }
    return res
  },
  error => {
    const that = getContext()
    console.error(error) // for debug
    if (!that) {
      if (error.message.includes('timeout')) {
        Message({
          message: '超时',
          type: 'error',
          showClose: true,
          duration: 5 * 1000
        })
      } else {
        Message({
          message: '网络出错,请稍后重试',
          type: 'error',
          showClose: true,
          duration: 5 * 1000
        })
      }
    } else {
      if (error.message.includes('timeout')) {
        Message({
          message: that.$t('outTime'),
          type: 'error',
          showClose: true,
          duration: 5 * 1000
        })
      } else {
        Message({
          message: that.$t('error') + error.message,
          type: 'error',
          showClose: true,
          duration: 5 * 1000
        })
      }
    }
    return Promise.reject(error)
  }
)

export default service
